package me.caofeng.controller.manager;

import me.caofeng.annotation.Authentication;
import me.caofeng.enums.ItemStatus;
import me.caofeng.enums.ResponseCode;
import me.caofeng.enums.Role;
import me.caofeng.pojo.Category;
import me.caofeng.pojo.Response;
import me.caofeng.pojo.User;
import me.caofeng.service.api.ICategoryService;
import me.caofeng.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.controller.manager
 * @Description: 分类管理
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-19 21:18
 * @Version: V1.0
 */
@Controller
@RequestMapping("manager/category")
@Authentication(role = Role.ADMIN)
public class ManagerCategoryController {

    @Autowired
    private ICategoryService categoryService;


    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public Response<List<Category>> getCategory(
            @PathVariable(value = "id") Integer id,
            @RequestParam(value = "status", required = false) Integer status) {
        Response<List<Category>> response = categoryService.getCategories(id, status,Role.ADMIN);
        return response;
    }

    /**
     * @Author:曹峰 blog.caofeng.me
     * @Date: 2017/6/19 23:44
     * @Description: 获取分类详情
     */
    @RequestMapping(value = "{parentId}/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Category> getCategory(@PathVariable("parentId") Integer parentId,
                                          @PathVariable("id") Integer id,
            @RequestParam(value = "status", required = false) Integer status) {
        return categoryService.getCategory(parentId, id, status,Role.ADMIN);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Response<Category> addCategory(@Valid Category category, BindingResult result) {
        Optional<ObjectError> error = result.getAllErrors()
                .stream().filter(objectError -> null != objectError).findFirst();

        if (error.isPresent()) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, error.get().getDefaultMessage(), null);
        }

        return categoryService.addCategory(category);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Response<Category> deleteCategory(@PathVariable("id") Integer id) {
        return categoryService.deleteCategory(id);
    }


    @RequestMapping(value = "{id}/name", method = RequestMethod.POST)
    @ResponseBody
    public Response<Category> updateCategoryName(@PathVariable("id") Integer id,
                                                 @RequestParam("name") String name) {
        return categoryService.updateCategoryName(id, name);
    }

    @RequestMapping(value = "{parentId}/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Response<Category> updateCategoryParentId(@PathVariable("id") Integer id,
                                                     @PathVariable("parentId") Integer parentId) {
        return categoryService.updateCategoryParentId(id, parentId);
    }


    @RequestMapping(value = "{id}/status", method = RequestMethod.POST)
    @ResponseBody
    public Response<Category> updateCategoryStatus(@PathVariable("id") Integer id,
                                                   @RequestParam("status") Integer status) {
        return categoryService.updateCategoryStatus(id, status);
    }

    @RequestMapping(value = "{id}/order", method = RequestMethod.POST)
    @ResponseBody
    public Response<Category> updateCategorySortOrder(@PathVariable("id") Integer id,
                                                      @RequestParam("order") Integer order) {
        return categoryService.updateCategorySortOrder(id, order);
    }
}

